package com.guangyuan.network.interval;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.guangyuan.network.constants.SqlType;
import com.guangyuan.network.dao.DBFactory;
import com.guangyuan.network.dao.interfaces.sql.ProductAttrSql;
import com.guangyuan.network.dataobject.ProductAttr;
import com.guangyuan.network.datasource.pool.util.ConnectionUtil;
import com.guangyuan.network.external.ProductAttrOper;

public class ProductAttrCache extends DefaultCache implements ProductAttrOper{
	
	private static ProductAttrCache productAttrCache;
	
	private ProductAttrCache() {
		
	}
	
	@Override
	public boolean deleteProductAttr(int id) {
		String[] parameters = new String[]{"" + id};
		
		Integer result = (Integer) execute(parameters, ProductAttrSql.delete, SqlType.OTHER);
		if(result > 0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean insertProductAttr(ProductAttr productAttr) {
		String[] parameters = new String[5];
		parameters[0] = productAttr.getName();
		parameters[1] = productAttr.getCnName();
		parameters[2] = productAttr.getDescription();
		parameters[3] = productAttr.getOperator();
		parameters[4] = productAttr.getProductGroupId().toString();
		
		Integer result = (Integer) execute(parameters, ProductAttrSql.insert, SqlType.INSERT);
		
		if(result > 0) {
			return true;
		}
		
		return false;
	}

	@Override
	public List<ProductAttr> listProductAttrByProductGroupId(int productGroupId) {
		String[] parameters = new String[]{"" + productGroupId};
		Connection conn = ConnectionUtil.getConnection();
		
		ResultSet rs =  (ResultSet) DBFactory.getDefaultDao().executeSql(conn, ProductAttrSql.selectByProductGroupId, parameters, SqlType.OTHER);
		List<ProductAttr> list = new ArrayList<ProductAttr>();
		if(null != rs) {
			
			try {
				while(rs.next()) {
					ProductAttr productAttr = new ProductAttr();
					productAttr.setId(rs.getInt(1));
					productAttr.setName(rs.getString(2));
					productAttr.setCnName(rs.getString(3));
					productAttr.setDescription(rs.getString(4));
					productAttr.setOperator(rs.getString(5));
					productAttr.setGmtCreate(rs.getDate(6));
					productAttr.setGmtModified(rs.getDate(7));
					productAttr.setProductGroupId(rs.getInt(8));
					list.add(productAttr);
				}
			} catch (SQLException e) {
				logger.error(e.getMessage());
			} finally {
				ConnectionUtil.closeConnection(conn);
			}
		}
		
		return list;
	}

	@Override
	public boolean updateProductAttr(ProductAttr productAttr) {
		String[] parameters = new String[5];
		parameters[0] = productAttr.getName();
		parameters[1] = productAttr.getCnName();
		parameters[2] = productAttr.getDescription();
		parameters[3] = productAttr.getOperator();
		parameters[4] = productAttr.getId().toString();
		
		Integer result = (Integer) execute(parameters, ProductAttrSql.update, SqlType.OTHER);
		if(result > 0) {
			return true;
		}
		
		return false;
	}

	public static ProductAttrCache getProductAttrCache() {
		if(null == productAttrCache) {
			productAttrCache = new ProductAttrCache();
		}
		return productAttrCache;
	}
}
